package com.greenhouse.business.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.greenhouse.business.domain.BizDeviceGroup;
import com.greenhouse.business.service.IBizDeviceGroupService;
import com.greenhouse.common.annotation.Log;
import com.greenhouse.common.core.controller.BaseController;
import com.greenhouse.common.core.domain.AjaxResult;
import com.greenhouse.common.core.page.TableDataInfo;
import com.greenhouse.common.enums.BusinessType;
import com.greenhouse.common.utils.poi.ExcelUtil;

/**
 * 设备_分组Controller
 * 
 * @author greenhouse
 * @date 2021-05-06
 */
@RestController
@RequestMapping("/business/devicegroup")
public class BizDeviceGroupController extends BaseController {
	@Autowired
	private IBizDeviceGroupService bizDeviceGroupService;

	/**
	 * 查询设备_分组列表
	 */
	@PreAuthorize("@ss.hasPermi('system:group:list')")
	@GetMapping("/list")
	public TableDataInfo list(BizDeviceGroup bizDeviceGroup) {
		startPage();
		List<BizDeviceGroup> list = bizDeviceGroupService.selectBizDeviceGroupList(bizDeviceGroup);
		return getDataTable(list);
	}

	/**
	 * 导出设备_分组列表
	 */
	@PreAuthorize("@ss.hasPermi('system:group:export')")
	@Log(title = "设备_分组", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(BizDeviceGroup bizDeviceGroup) {
		List<BizDeviceGroup> list = bizDeviceGroupService.selectBizDeviceGroupList(bizDeviceGroup);
		ExcelUtil<BizDeviceGroup> util = new ExcelUtil<BizDeviceGroup>(BizDeviceGroup.class);
		return util.exportExcel(list, "group");
	}

	/**
	 * 获取设备_分组详细信息
	 */
	@PreAuthorize("@ss.hasPermi('system:group:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(bizDeviceGroupService.selectBizDeviceGroupById(id));
	}

	/**
	 * 新增设备_分组
	 */
	@PreAuthorize("@ss.hasPermi('system:group:add')")
	@Log(title = "设备_分组", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody BizDeviceGroup bizDeviceGroup) {
		return toAjax(bizDeviceGroupService.insertBizDeviceGroup(bizDeviceGroup));
	}

	/**
	 * 修改设备_分组
	 */
	@PreAuthorize("@ss.hasPermi('system:group:edit')")
	@Log(title = "设备_分组", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody BizDeviceGroup bizDeviceGroup) {
		return toAjax(bizDeviceGroupService.updateBizDeviceGroup(bizDeviceGroup));
	}

	/**
	 * 删除设备_分组
	 */
	@PreAuthorize("@ss.hasPermi('system:group:remove')")
	@Log(title = "设备_分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(bizDeviceGroupService.deleteBizDeviceGroupByIds(ids));
	}

	@PreAuthorize("@ss.hasPermi('system:group:edit')")
	@Log(title = "设备_分组", businessType = BusinessType.UPDATE)
	@PutMapping("/unBound")
	public AjaxResult unBound(@RequestBody BizDeviceGroup bizDeviceGroup) {
		List<BizDeviceGroup> list = bizDeviceGroupService.selectBizDeviceGroupList(bizDeviceGroup);
		list.forEach(m -> {
			bizDeviceGroupService.deleteBizDeviceGroupById(m.getDeviceId());
		});

		return AjaxResult.success();
	}

}
